package com.eshopkeeper.provider.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.plugins.Page;
import com.eshopkeeper.core.base.BaseProviderImpl;
import com.eshopkeeper.core.util.InstanceUtil;
import com.eshopkeeper.dao.sys.SysMenuBtnMapper;
import com.eshopkeeper.dao.sys.SysMenuMapper;
import com.eshopkeeper.dao.sys.SysRoleMapper;
import com.eshopkeeper.dao.sys.SysRoleMenuMapper;
import com.eshopkeeper.model.sys.SysMenuBtn;
import com.eshopkeeper.model.sys.SysRole;
import com.eshopkeeper.model.sys.ext.SysMenuBean;
import com.eshopkeeper.model.sys.ext.SysRoleBean;
import com.weibo.api.motan.config.springsupport.annotation.MotanService;

/**
 * @author chenshufeng
 * @version 2017年3月17日31日 上午11:01:33
 */
////@CacheConfig(cacheNames = "sysRole")
@MotanService(basicService = "serviceBasicConfig")
public class SysRoleProviderImpl extends BaseProviderImpl<SysRole> implements ISysRoleProvider {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;
	@Autowired
	private SysMenuBtnMapper sysMenuBtnMapper;

    public Page<SysRole> query(Map<String, Object> params) {
        Page<Long> page = this.getPage(params);
        page.setRecords(mapper.selectIdByMap(page, params));
        return getPage(page);
    }

    public Page<SysRoleBean> queryBean(Map<String, Object> params) {
        Page<Long> idPage = this.getPage(params);
        idPage.setRecords(mapper.selectIdByMap(idPage, params));
        Page<SysRoleBean> pageInfo = getPageNotFromRedis(idPage, SysRoleBean.class);
        // 权限信息
        for (SysRoleBean bean : pageInfo.getRecords()) {
            List<String> permissions = sysRoleMenuMapper.queryPermission(bean.getId());
            for (String permission : permissions) {
                if (StringUtils.isBlank(bean.getPermission())) {
                    bean.setPermission(permission);
                } else {
                    bean.setPermission(bean.getPermission() + ";" + permission);
                }
            }
        }
        return pageInfo;
    }

    /* (non-Javadoc)
     * @see com.eshopkeeper.provider.SysRoleProvider#getPermissions(java.lang.String) */
    @Override
    public List<String> getPermissions(Long id) {
        return sysRoleMenuMapper.getPermissions(id);
    }
    
    public void insertSysRole(SysRole sysRole) {
    	sysRoleMapper.insertSysRole(sysRole);
    }
    
	public void updateSysRole(Map<String, Object> paramMap) {
    	sysRoleMapper.updateSysRole(paramMap);
	}

	@Override
	public List<SysMenuBean> getMenuList(Long id) {
		List<SysMenuBean> menus = sysMenuMapper.querySysMenuList(id);
		Map<Long, List<SysMenuBean>> map = InstanceUtil.newHashMap();
		for (SysMenuBean sysMenuBean : menus) {
			if (map.get(sysMenuBean.getParentId()) == null) {
				List<SysMenuBean> menuBeans = InstanceUtil.newArrayList();
				map.put(sysMenuBean.getParentId(), menuBeans);
			}
			map.get(sysMenuBean.getParentId()).add(sysMenuBean);
		}
		List<SysMenuBean> result = InstanceUtil.newArrayList();
		for (SysMenuBean sysMenuBean : menus) {
			if (sysMenuBean.getParentId() == 0) {
				sysMenuBean.setLeaf(0);
				sysMenuBean.setMenuBeans(getChildMenu(map, sysMenuBean.getId()));
				result.add(sysMenuBean);
			}
		}
		return result;
	}
	
	@Override
	public List<SysMenuBean> getMenuFuncList(Long id) {
		List<SysMenuBean> menus = sysMenuMapper.queryMenuFuncList(id);
		List<SysMenuBtn> menusBtn=sysMenuBtnMapper.getListBtnByrole(id);
		Map<Long, List<SysMenuBean>> map = InstanceUtil.newHashMap();

		for (SysMenuBean sysMenuBean : menus) {
			if (map.get(sysMenuBean.getParentId()) == null) {
				List<SysMenuBean> menuBeans = InstanceUtil.newArrayList();
				map.put(sysMenuBean.getParentId(), menuBeans);
			}
			map.get(sysMenuBean.getParentId()).add(sysMenuBean);
		}
		List<SysMenuBean> result = InstanceUtil.newArrayList();
		for (SysMenuBean sysMenuBean : menus) {
			if (sysMenuBean.getParentId() == 0) {
				sysMenuBean.setLeaf(0);
				List<SysMenuBean> menuBeans=getChildMenu(map, sysMenuBean.getId());
				List <SysMenuBtn> meunsbtn=null;
				List<SysMenuBean> menuBeansChild=new ArrayList<>();
				for (SysMenuBean sysMenuBeanchild : menuBeans) {
					meunsbtn=new ArrayList<>();
					for (SysMenuBtn sysMenuBtn : menusBtn) {
						if (sysMenuBtn.getMenuId().toString().equals(sysMenuBeanchild.getId().toString()) ) {
							meunsbtn.add(sysMenuBtn);
						}
					}
					sysMenuBeanchild.setMenuBtnBeans(meunsbtn);
					menuBeansChild.add(sysMenuBeanchild);
				}
				sysMenuBean.setMenuBeans(menuBeansChild);
				/*logger.info("二级菜单："+ JSONArray.parse(sysMenuBean.getMenuBeans().toString()).toString());*/
				result.add(sysMenuBean);
			}
		}
		return result;
	}
	
	// 递归获取子菜单
	private List<SysMenuBean> getChildMenu(Map<Long, List<SysMenuBean>> map, Long id) {
		List<SysMenuBean> menus = map.get(id);
		if (menus != null) {
			for (SysMenuBean sysMenuBean : menus) {
				sysMenuBean.setMenuBeans(getChildMenu(map, sysMenuBean.getId()));
			}
		}else{
			menus=new ArrayList<>();
		}
		return menus;
	}
	
    //查询权限角色列表
	@Override
    public List<Map<String, Object>> selSysRoleList() {
        return sysRoleMapper.selSysRoleList();
    }
    
    //删除角色菜单信息
    @Override
    public int deleteRoleMenuById(long id){
        return sysRoleMapper.deleteRoleMenuById(id);
    }
    //批量添加角色菜单信息
    @Override
    public int batchAddRoleMenu(List<String> menuIds,Long roleId){
    	List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
    	for(int i=0;i<menuIds.size();i++){
    		Map<String,Object> map= new HashMap<String, Object>();
        	map.put("menuId", menuIds.get(i));
        	map.put("roleId",roleId);
        	list.add(map);
    	}
        return sysRoleMapper.insertMenuRole(list);
    }
  //删除角色菜单功能信息
    @Override
    public int deleteRoleMenuFuncById(long id){
        return sysRoleMapper.deleteRoleMenuFuncById(id);
    }

	@Override
	public int batchAddRoleMenuFunc(List<Map<String, Object>> list) {
		return sysRoleMapper.insertMenuRoleFunc(list);
	}
}
